<?php 
class menuPer{
	
    public $fileXml='menu.xml';
    
    private $dohide='hide';
    
    public $xmlOpen;
    
    public $group_id;
    
    public $Ary=array();
    
    #打開XML
    public function __construct(){
    	global $config;

    	if(!is_file(HZHIHU_ROOT.$config->cachedir.'xml/'.$this->fileXml)){
    		exit('找不到'.HZHIHU_ROOT.$config->cachedir.'xml/'.$this->fileXml);
    	}
    	$this->xmlOpen=simplexml_load_file(HZHIHU_ROOT.$config->cachedir.'xml/'.$this->fileXml);
		if(sizeof($this->xmlOpen->menu)==1){
			$this->xmlOpen=$this->xmlOpen->menu[0];
		}    	
    }
    
    #寫入菜單權限
    public function perXml(){
    	$adminfileClass=new adminfile();
    	$top=$sen=$topAry=$senAry=array();
    	$filename='';
    	foreach($this->xmlOpen->menu as $topMenu){
    		$id=(int)$topMenu->id;
    		if(!empty($id)){
	    		$title=(string)$topMenu->title;	    		
	    		$topAry=array('title'=>$title
	    					  ,'pid'=>0
	    					  ,'s_erg'=>1
	    					  ,'a_erg'=>1
	    					  ,'e_erg'=>1
	    					  ,'d_erg'=>1
	    					  ,'hide'=>0
	    					  ,'filename'=>$filename
	    					  ,'group_id'=>$this->group_id
	    					 );
	    		$top[]=$topAry;
	    		$pid=$adminfileClass->add($topAry);
    		}
    		foreach ($topMenu as $senMenu){
    			$id=(int)$senMenu->id;
    			$title=(string)$senMenu->title;
    			$hide=(int)$senMenu->hide;
    			$class=(string)$senMenu->class;
    			$do=(string)$senMenu->do;
    			$action=(string)$senMenu->action;
    			$filename=$class.'.'.$action.'.php';
				if($do=='do'){
				 	$filename=$action.'.php';
				}    			
    			if(!empty($id)){
    				$url=$this->perUrl($senMenu);
    				$url=$url;
    				$senAry=array('title'=>$title
    							  ,'url'=>$url
    							  ,'pid'=>$pid
    							  ,'s_erg'=>1
    							  ,'a_erg'=>1
    							  ,'e_erg'=>1
    							  ,'d_erg'=>1
    							  ,'hide'=>$hide
    							  ,'filename'=>$filename
    							  ,'group_id'=>$this->group_id
    							 );
    				$sen[$id]=$senAry;
    				$adminfileClass->add($senAry);
    			}
    		}
    	}
    	$senAry=array('title'=>'後台首頁','url'=>'admin.php?do=home','pid'=>$pid,'s_erg'=>1,'a_erg'=>1,'e_erg'=>0,'d_erg'=>0,'hide'=>1,'filename'=>'home.php','group_id'=>$this->group_id);
    	$adminfileClass->add($senAry);
    }
    
    #右邊菜單[二級菜單]
    public function admin_indexmenu(){
        global $config;
        if($config->perFile){
        	$adminfileClass=new adminfile();
        	$memberClass=new member();
        	$memberClass->wheres='id="'.$_SESSION['ADMIN_ID'].'"';
        	$list=$memberClass->getList();
        	$group_id=$list[0]['group_id'];    	
        	$List=$adminfileClass->setWheregroup_id($group_id);
        }else{
            $List=$this->xmlOpen;
        }
        $f1=false;
		foreach($List as $topMenu){
		     $topMenu=(array)$topMenu;
		    if($config->perFile){ 
    		 	if(!$topMenu['s_erg']){
    		 		continue;
    		 	}
		     }
			$html.='<div topMid="'.$topMenu['id'].'" class="RightCenter" ';
			if(isset($_COOKIE['topMenu'])){ 
			    if($_COOKIE['topMenu']!=$topMenu['id']){
			          $html.= 'style="display: none;" '; 
			    }
			}else{
			    if($f1){
			         $html.= 'style="display: none;" '; 
			    }
			}
			$html.='>';
			$f1=true;
			if(is_array($topMenu['menu'])){
				foreach($topMenu['menu'] as $menu){
				    $menu=(array)$menu;
				    if($config->perFile){ 
    					if(!$menu['s_erg']){
    						continue;
    					}	
				    }
				    if($menu['hide']){
				        continue;
				    }
					$right=true;
					$html.='<ul>
						<li class="Title"><h3 mid="'.$menu['id'].'" href="'.$this->perUrl($menu).'" class="mouse">'.$menu['title'].'</h3></li>
						<li class="Content">';
						if(is_array($menu['menu'])){
							foreach($menu['menu'] as $subMenu){
							$subMenu=(array)$subMenu;
							$html.='<ul>
									<li mid="'.$subMenu['id'].'" href="'.$this->perUrl($menu).'" class="mouse">'.$subMenu['title'].'</li>
									</ul>';
								}
						}
					$html.='</li></ul>';
				}
			}
			$html.='</div>';
			if(!$right){
				$html.="<script>
					function pageOver(){
						$('.MainRight').hide();
						$('.MainLeft').width('100%');
						$('#frames').width('100%');
					}
					</script>";
			}
		}
    	return $html;
    }
    
    #頂級菜單
    public function topmenu(){
        global $config;
        if($config->perFile){
        	$adminfileClass=new adminfile();
        	$memberClass=new member();
        	$memberClass->wheres='id="'.$_SESSION['ADMIN_ID'].'"';
        	$list=$memberClass->getList();
        	$group_id=$list[0]['group_id'];
        	$list=$adminfileClass->setWheretop($group_id);
        }else{
            $list=$this->xmlOpen;
        }       	   
	   $html='<ul id="topMenu">';
    	   foreach($list as $topMenu){
    	       $topMenu=(array)$topMenu;   	      
    	       if($config->perFile){
        		 	if(!$topMenu['s_erg']){
        		 		continue;
        		 	}    	
    	        }	
    		    $url=$this->perUrl($topMenu);        
        	    $html.='<li mid="'.$topMenu['id'].'"';
        	 	if(!$first && !isset($_COOKIE['topMenu'])){
        	 	    $html.='class="Current"';
        	 	    $first=true;
        	 	 }else{
        	 	     if($topMenu['id']==$_COOKIE['topMenu']){
        	 	         $html.='class="Current"';
        	 	         $first=true;
        	 	     }
        	 	 }
        		$html.='><a mid="'.$topMenu['id'].'" href="'.$url.'">'.$topMenu['title'].'</a></li>';
	    }
	    $html.='</ul>';
    	return $html;
    }
    
    #返回所有菜單的HTML
    public function Hmenu(){
		$html=$this->CenMenu($this->xmlOpen);	
		return $html;
	}
	
	#遞歸所有菜單，得到HTML
	public function CenMenu($xmlAry){
		foreach ($xmlAry as $k=>$xml){
			if(isset($xml->title) && !empty($xml->title)){				
				if($xml->menu){
			     	$html.='<li><a href="javascript:void(0);">'.$xml->title.'</a><ul>'.$this->CenMenu($xml).'</ul></li>';	
				}else{
				    $html.='<li><a href="'.$this->perUrl($xml).'">'.$xml->title.'</a></li>';
				}
			}
			
		}		
		return $html;
	}
    
	
    private  function editPer($filename){
	    $adminfileClass=new adminfile();
	    $list=$adminfileClass->setWhereado($filename);
	    $info=$list[0];
	    if(empty($list)){
	    		exit('非法錯誤!');
	    }else{
	    	$this->perAlert('edit',$info);
	    }
    }
    
    private  function addPer($filename){
    	$adminfileClass=new adminfile();
    	$list=$adminfileClass->setWhereado($filename);
    	$info=$list[0];
    	if(empty($list)){
    		exit('非法錯誤!');
    	}else{
    		$this->perAlert('add',$info);
    	}  	
    }
    
   public   function selectPer($url=''){
   		if(empty($url)){
   			$url=$this->urla();
   		}	

	    $adminfileClass=new adminfile();
    	$memberClass=new member();
    	$memberClass->wheres='id="'.$_SESSION['ADMIN_ID'].'"';
    	$list=$memberClass->getList();
    	$group_id=$list[0]['group_id'];	    
    	
	    $list=$adminfileClass->setWhereaurl($url,$group_id);
	    $info=$list[0];
	    if(basename($_SERVER['PHP_SELF'])=='admin.php'){
		    if(empty($list)){
		        
		    	exit('你沒有權限 加載此頁，請聯繫管理 員!');
		    	
		    }else{
		    	$this->perAlert('select',$info);
		    } 
	    }  	
    }
    
    public function urla(){
    	 parse_str($_SERVER['QUERY_STRING'],$outurl);
         $checkarray=array('type','do','cn');
         $url=$furl=array();
         foreach ($outurl as $k=>$val){
       		  if(in_array($k,$checkarray)){       		   
       		  	$url[$k]=$val;
       		  }
         }
       $furl=array('type'=>$url['type'],'do'=>$url['do'],'cn'=>$url['cn']);
       $surl=substr($_SERVER['PHP_SELF'],1).'?'.http_build_query($furl);
       $surl=$surl;
       return $surl;
    }
    
    private function perAlert($type='edit',$Ary){
    	$href=$_SERVER['HTTP_REFERER'];
    	switch ($type){
    		case 'edit':
    			if(!$Ary['e_erg']){
    				jsCtrl::Alert_Location('您沒有此操作的權限,請聯繫管理員!',$href);
    				exit();
    			}
    			break;
    		case 'add':
    			if(!$Ary['a_erg']){
    				jsCtrl::Alert_Location('您沒有此操作的權限,請聯繫管理員!',$href);
    				exit();
    			}
    			break;
    		case 'select':
    			if(!$Ary['s_erg']){
    				jsCtrl::Alert('您沒有此操作的權限,請聯繫管理員!');
    				exit();    				
    			}
    			break;
    		case 'del':
    			if(!$Ary['d_erg']){
    				jsCtrl::Alert_Location('您沒有此操作的權限,請聯繫管理員!',$href);
    				exit();
    			}
    			break;
    		default:
    			jsCtrl::Alert('非法操作!',$url);
    			exit();    			
    		    break;
    	}
    }
    
    public function Actiongroup($member_id,$group_id=''){   	
    	$defaue=12;
    	if(empty($group_id)){
    		$memberClass=new member();
    		$memberClass->wheres='id="'.$member_id.'"';
    		$memberList=$memberClass->getList();  
    				
    		if(empty($memberList)){
    			exit('發生錯誤1');
    		}else{
    			$group_id=$memberList[0]['group_id'];
 
    			if(empty($group_id)){
    				$array=array('group_id'=>$group_id);
    				$memberClass->edit($array,$member_id);
					$this->group_id=$defaue;
					$this->perXml(); 				
					$group_id=$defaue;
    			}   			
    		}
    	}
    	$groupClass=new group();
    	$groupClass->wheres='id="'.$group_id.'"';
    	$groupList=$groupClass->getList();

    	if(empty($groupList)){
    		$groupList=$groupClass->setList();
    		$group_id=$groupList[0]['id'];  		
			$array=array('group_id'=>$group_id);
			$memberClass->edit($array,$member_id);    		
    	}

    	$adminfileClass=new adminfile();
    	$list=$adminfileClass->setWheretop($group_id);
    	if(empty($list)){
    		exit('發生錯誤2!');
    	}else{
    		foreach ($list as $tk=>&$val){
    			$pidList=$adminfileClass->setWherepid($val['id']);
    			if(!empty($pidList)){
    				$val['pid_group']=$pidList;
    			}
    		}
    	}
    	return $list;
    }
    
    
    
    public function perUrl($menu){
        if(is_array($menu)){
            $menu=(object)$menu;
        }
		$do=$menu->do;
		$action=$menu->action;
		$class=$menu->class;
		$param=$menu->param;
	    if($do=='do'){
	        return $url='admin.php?do='.$action;
	    }
		if($action==''){
			return '';
		}else{
			$url='admin.php?type='.$do.'&do='.$action.'&cn='.$class;
			if($param) $url.='&'.$param;
			return $url;
		}    	
    }
    
    
    
}